#include "Exportar.h"

void exporter(Scene *scene){
        vector <Vertice *> vertices;
        vector<Vertex3D *> aux_v;
        vector<Triangle *> triangulos;
        FILE *fd=fopen("./scene.xml","w");
        Camera *cam=new Camera(scene->get_cam()->pos(),scene->get_cam()->look(),scene->get_cam()->up());
        int i=0,j,sp=0,v=0,v1=0,v2=0,v3=0;
        fprintf(fd,"<?xml version=1.0?>\n");
        putspaces(++sp,fd);
        fprintf(fd,"<scene>\n");
        vector<Object3D *>objects=scene->get_objects();
        for (vector<Object3D *>::iterator it = objects.begin (); it != objects.end (); it ++){
                int n_verts=length((*it)->get_verts());
                int n_faces=length((*it)->get_triangs());
                putspaces(++sp,fd);
                fprintf(fd,"<mesh vertices=\"%d \" faces=\"%d \">\n",n_verts,n_faces);
                aux_v=(*it)->get_verts();
                for(vector<Vertex3D *>::iterator ite = aux_v.begin(); ite!= aux_v.end(); ite++){
                        vertices[v]=new Vertice(aux_v[v],v);
                        v++;
                }
                sp++;   
                for(j=0;j<n_verts;j++){
                        putspaces(sp,fd);
                        fprintf(fd,"<p x=\"%f \" y=\"%f \" z=\"%f \"/>\n",vertices[j]->x(),vertices[j]->y(),vertices[j]->z()) ;
                }
                triangulos=(*it)->get_triangs();
                for(j=0;j<n_faces;j++){
                        putspaces(sp,fd);
                        Vertice *v1_aux=new Vertice(triangulos[j]->vertex(0),0);
                        Vertice *v2_aux=new Vertice(triangulos[j]->vertex(1),0);
                        Vertice *v3_aux=new Vertice(triangulos[j]->vertex(2),0);
                        while (v1==0&&v2==0&&v3==0&&i<v){
                                if(vertices[i]==v1_aux){
                                        v1_aux->set_index(vertices[i]->get_index());
                                        v1=1;
                                }else if(vertices[i]==v2_aux){
                                        v2_aux->set_index(vertices[i]->get_index());
                                        v2=1;
                                }else if(vertices[i]==v3_aux){
                                        v3_aux->set_index(vertices[i]->get_index());
                                        v3=1;
                                }
                                i++;
                        }
                        fprintf(fd,"<f a=\"%d \" b=\"%d \" c=\"%d \"/>\n",v1_aux->get_index(),v2_aux->get_index(),v3_aux->get_index()) ;
                }
        putspaces(--sp,fd);
        fprintf(fd,"</mesh>\n\n");
        }
        putspaces(sp,fd);       
        fprintf(fd,"<camera>\n");
        putspaces(++sp,fd);
        fprintf(fd,"<from p x=\"%f \" y=\"%f \" z=\"%f \" />\n",cam->pos_x(),cam->pos_y(),cam->pos_z());
        putspaces(sp,fd);
        fprintf(fd,"<to p x=\"%f \" y=\"%f \" z=\"%f \" />\n",cam->look_x(),cam->look_y(),cam->look_z());
        putspaces(sp,fd);
        fprintf(fd,"<up p x=\"%f \" y=\"%f \" z=\"%f \" />\n",cam->up_x(),cam->up_y(),cam->up_z());
        putspaces(--sp,fd);
        fprintf(fd,"</camera>\n\n");
        fclose(fd);
}

static void putspaces(int sp,FILE *fd)
  {
    int i;
    for(i=0;i<sp;i++)
    {
      fprintf(fd,"\t");
    }
  }

